package cn.tedu.csmall.product.redis;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Slf4j
@SpringBootTest
public class redistext {
    @Autowired
    RedisTemplate<String,Serializable> redisTemplated;
    @Test
    void valueSet(){
        String key ="username11";
        String value ="mcl";

        ValueOperations<String, Serializable> ops=redisTemplated.opsForValue();
        ops.set(key,value);
    }
    @Test
    void valueGet(){
        String key ="username11";

        ValueOperations<String,Serializable> ops=redisTemplated.opsForValue();
        Serializable value =ops.get(key);
        log.info("从Redis中取出Key为{}的数据,结果:{}",key,value);
    }
    @Test
    void keys(){
        String pattern ="*";
        Set<String> keys =redisTemplated.keys(pattern);
        log.debug("根据模式[{}]搜索Key,结果:{}",pattern,keys);
    }
    @Test
    void delete(){
        String key ="username1";
    }

    @Test
    void rightPush(){
        String key ="stringList";
        List<String> stringList = new ArrayList<>();
        for (int i = 0; i <= 10; i++) {
            stringList.add("string-"+i);
        }
        ListOperations<String, Serializable> ops = redisTemplated.opsForList();
        for (String s : stringList) {
            ops.rightPush(key, s);
        }
    }
    @Test
    void size(){
        String key ="stringList";
        ListOperations<String,Serializable> ops =redisTemplated.opsForList();
        Long size =ops.size(key);
        log.debug("{}"+size);

    }
    @Test
    void range(){
        String key ="stringList";
        long start =-9;
        long end =9;
        ListOperations<String,Serializable> ops =redisTemplated.opsForList();
        List<Serializable> list = ops.range(key,start,end);
        log.debug("根据Key【{}】从【{}】到【{}】读取列表，结果长度：{}", key, start, end, list.size());
        for (Serializable item : list) {
            log.debug("列表项:{}",item);
        }
    }



    void members(){
        SetOperations<String,Serializable> ops =redisTemplated.opsForSet();
        String key ="brandKeys";
        Set<Serializable> members =ops.members(key);
        log.debug("根据key[{}]读取Set数据,结果:{}",key,members);
    }


}
